﻿#ifndef __SS_TEMP_PTR_H__
#define __SS_TEMP_PTR_H__
#include "../ssBaseDefine.h"

namespace StarSeeker
{

//该容器一般只用于栈上的临时指针
template<typename T>
class ssTempPtr
{
public:
	explicit ssTempPtr(T* p)
	{
		m_ptr = p;
	}
	~ssTempPtr()
	{
		SS_SAFE_DELETE(m_ptr)
	}
	T* operator -> ()
	{
		return m_ptr;
	}
	T& operator * ()
	{
		return *m_ptr;
	}
	T* Get()
	{
		return m_ptr;
	}
private:
	ssTempPtr()
	{
		m_ptr = 0;
	}
	ssTempPtr(const ssTempPtr& up)
	{
		m_ptr = 0;
	}
	void operator = (const ssTempPtr& up)
	{
		m_ptr = 0;
	}

private:
	T*	m_ptr;
};

//不推荐这个用法，最好用数组容器
template<typename T>
class ssTempArrPtr
{
public:
	explicit ssTempArrPtr(u32 len)
	{
		m_ptr = new T[len];
		m_len = len;
	}
	explicit ssTempArrPtr(T* arr, u32 len)
	{
		m_ptr = arr;
		m_len = len;
	}
	~ssTempArrPtr()
	{
		SS_SAFE_DEL_ARRAY(m_ptr)
	}
	T& operator [] (u32 offset)
	{
		if (m_ptr)
		{
			if (offset < m_len)
			{
				return m_ptr[offset];
			}
			else
			{
				SS_THROW("[] out of range");
			}
		} 
		else
		{
			SS_THROW("[] null pointer");
		}
	}
	T* Get()
	{
		return m_ptr;
	}
private:
	ssTempArrPtr()
	{
		m_ptr = 0;
		m_len = 0;
	}
	ssTempArrPtr(const ssTempArrPtr& up)
	{
		m_ptr = 0;
		m_len = 0;
	}
	void operator = (const ssTempArrPtr& up)
	{
		m_ptr = 0;
		m_len = 0;
	}

private:
	T*		m_ptr;
	u32		m_len;
};

}
#endif
